from tqdm import tqdm
from helpers import utils
from helpers import datetime
from data_analysis import keywords_extractor

def analyze_data(date, enable_bar=True):
    raw_data = utils.read_json(utils.get_path('RAW_DATA_PATH', date))
    if not raw_data:
        return
    keywords = keywords_extractor.get_keywords(__get_questions(raw_data), enable_bar)
    utils.write_json(utils.get_path('KEYWORDS_PATH', date), keywords)

def analyze_data_in_range(start, end):
    for date in tqdm(datetime.range_iterator(start, end), desc='分析数据', unit='天'):
        analyze_data(date, False)

def __get_questions(data):
    questions = []
    for item in data:
        question = item.get('问题描述')
        if question:
            questions.append(question)
    return questions
